package com.um.psystem.service.fixtureService.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.um.psystem.entity.fixtureEntity.FixtureDetail;
import com.um.psystem.entity.fixtureEntity.FixtureType;
import com.um.psystem.entity.mtEntity.AssetsDetail;
import com.um.psystem.mapper.platform.PublicMapper;
import com.um.psystem.mapper.platform.fixtureManageMapper.FixtureDetailMapper;
import com.um.psystem.mapper.platform.fixtureManageMapper.FixtureTypeMapper;
import com.um.psystem.model.vo.JsonResult;
import com.um.psystem.service.fixtureService.IFixtureTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: AiLen2E
 * @Date: 2022/1/20 14:57
 * @version: 1.0
 */
@Service
public class FixtureTypeService implements IFixtureTypeService {

    @Autowired
    FixtureTypeMapper fixtureTypeMapper;

    @Autowired
    FixtureDetailMapper fixtureDetailMapper;

    @Autowired
    PublicMapper publicMapper;

    @Override
    public JsonResult<Integer> save(FixtureType fixtureType) {
        Integer insertNum =  fixtureTypeMapper.insert(fixtureType);
        return JsonResult.success(insertNum);
    }

    @Override
    public JsonResult<Integer> update(FixtureType fixtureType) {
        Integer updateNum =  fixtureTypeMapper.updateById(fixtureType);
        return JsonResult.success(updateNum);
    }

    @Override
    public JsonResult<Integer> del(Integer id) {
        Integer delNum = fixtureTypeMapper.delete(new EntityWrapper<FixtureType>()
                .eq("type_main_id",id));
        return JsonResult.success(delNum);
    }

    @Override
    public JsonResult<FixtureType> get(Integer id) {
        FixtureType fixtureType =  fixtureTypeMapper.selectById(id);
        return JsonResult.success(fixtureType);
    }

    @Override
    public List<FixtureType> getFixtureTypes(Map<String, Object> columnMap) {
        EntityWrapper<FixtureType> ew = new EntityWrapper<FixtureType>();
        if(StrUtil.isNotBlank(columnMap.get("typeMainName")!=null?columnMap.get("typeMainName").toString():null)){
            ew.like("type_main_name",columnMap.get("typeMainName").toString());
        }
        List<FixtureType> fixtureTypeList =  fixtureTypeMapper.selectList(ew);
        return fixtureTypeList;
    }

    @Override
    public JsonResult<Integer> save_fd(FixtureDetail fixtureDetail) {
        return JsonResult.success(fixtureDetailMapper.insert(fixtureDetail));
    }

    @Override
    public JsonResult<Integer> update_fd(FixtureDetail fixtureDetail) {
        return JsonResult.success(fixtureDetailMapper.updateById(fixtureDetail));
    }

    @Override
    public JsonResult<Integer> del_fd(Integer id) {
        Integer delNum = fixtureDetailMapper.delete(new EntityWrapper<FixtureDetail>()
                .eq("type_dtl_id",id));
        return JsonResult.success(delNum);
    }

    @Override
    public JsonResult<FixtureDetail> get_fd(Integer id) {
        FixtureDetail fixtureDetail =  fixtureDetailMapper.selectById(id);
        return JsonResult.success(fixtureDetail);
    }

    @Override
    public List<FixtureDetail> getFixtureDetails(Map<String, Object> columnMap) {
        EntityWrapper<FixtureDetail> ew = new EntityWrapper<FixtureDetail>();
        if(StrUtil.isNotBlank(columnMap.get("type_main_id")!=null?columnMap.get("type_main_id").toString():null)){
            ew.eq("type_main_id",columnMap.get("type_main_id").toString());
        }
        if(StrUtil.isNotBlank(columnMap.get("type_dtl_name")!=null?columnMap.get("type_dtl_name").toString():null)){
            ew.like("type_dtl_name",columnMap.get("type_dtl_name").toString());
        }
        List<FixtureDetail> fixtureDetails =  fixtureDetailMapper.selectList(ew);
        return fixtureDetails;
    }

    @Override
    public String getTypeDtlNo(Map<String, Object> columnMap) {
        Map paramsMap = new HashMap();
        String sql = "select CONCAT(b.type_main_no,lpad(a.type_dtl_num+1,4,0),'X') as typeDtlNo_new " +
                "from ws_eng_fixture_type_dtl a " +
                "join ws_eng_fixture_type_main b " +
                "on a.type_main_id=b.type_main_id " +
                "where b.type_main_id=#{type_main_id} " +
                "order by a.type_dtl_num desc limit 1";
        paramsMap.put("sqlStr",sql);
        paramsMap.put("type_main_id",columnMap.get("type_main_id").toString());
        List<LinkedHashMap<String, Object>> typeList = publicMapper.getPublicItems(paramsMap);
        if (typeList.size()>0){
            return typeList.get(0).get("typeDtlNo_new").toString();
        }else {
            sql = "select type_main_no from ws_eng_fixture_type_main where type_main_id=#{type_main_id}";
            paramsMap.put("sqlStr",sql);
            List<LinkedHashMap<String, Object>> typeMain_list = publicMapper.getPublicItems(paramsMap);
            return typeMain_list.get(0).get("type_main_no").toString()+"0001X";
        }
    }
}
